package leetcode.strings;

import java.util.ArrayList;
import java.util.List;

/**
 * 给定两个以字符串形式表示的非负整数 num1 和 num2，返回 num1 和 num2 的乘积，它们的乘积也表示为字符串形式。
 * <p>
 * 示例 1:
 * <p>
 * 输入: num1 = "2", num2 = "3"
 * 输出: "6"
 * 示例 2:
 * <p>
 * 输入: num1 = "123", num2 = "456"
 * 输出: "56088"
 * 说明：
 * <p>
 * num1 和 num2 的长度小于110。
 * num1 和 num2 只包含数字 0-9。
 * num1 和 num2 均不以零开头，除非是数字 0 本身。
 * 不能使用任何标准库的大数类型（比如 BigInteger）或直接将输入转换为整数来处理。
 */
public class Solution1 {
    public String multiply(String num1, String num2) {
        StringBuilder sb1 = new StringBuilder(num1);
        sb1 = sb1.reverse();
        num1 = sb1.toString();
        StringBuilder sb2 = new StringBuilder(num2);
        sb2 = sb2.reverse();
        num2 = sb2.toString();
        List<String> eachResult = new ArrayList<>();
        for (int i = 0; i <= num1.toCharArray().length - 1; i++) {
            int n1 = Integer.valueOf(String.valueOf(num1.charAt(i)));
            int factor = 0;
            String s = "";
            for (int j = 0; j <= num2.toCharArray().length - 1; j++) {
                int m1 = Integer.valueOf(String.valueOf(num2.charAt(j)));
                int pos = n1 * m1 + factor;
                if (j == num2.toCharArray().length - 1) {
                    s = pos + s;
                } else if (pos / 10 > 0) {
                    factor = pos / 10;
                    s = pos % 10 + s;
                } else {
                    s = pos + s;
                    factor = 0;
                }
            }
            for (int k = 0; k < i; k++) {
                s += '0';
            }
            eachResult.add(s);
        }
        String result = "";
        System.out.println(eachResult);
        for (String s : eachResult) {
            result = add(result, s);
        }
        boolean allZero = true;
        for (char c : result.toCharArray()) {
            if (c != '0') {
                allZero = false;
            }
        }
        if (allZero) {
            return "0";
        }
        return result;
    }

    public String add(String num1, String num2) {
        StringBuilder sb1 = new StringBuilder(num1);
        sb1 = sb1.reverse();
        num1 = sb1.toString();
        StringBuilder sb2 = new StringBuilder(num2);
        sb2 = sb2.reverse();
        num2 = sb2.toString();
        int len1 = num1.length();
        int len2 = num2.length();
        int maxLen = Math.max(len1, len2);
        int factor = 0;
        String s = "";
        for (int i = 0; i < maxLen; i++) {
            int n1;
            if (i > len1 - 1) {
                n1 = 0;
            } else {
                n1 = Integer.valueOf(String.valueOf(num1.charAt(i)));
            }

            int m1;
            if (i > len2 - 1) {
                m1 = 0;
            } else {
                m1 = Integer.valueOf(String.valueOf(num2.charAt(i)));
            }
            int pos = n1 + m1 + factor;

            if (i == maxLen - 1) {
                s = pos + s;
            } else if (pos / 10 > 0) {
                factor = pos / 10;
                s = pos % 10 + s;
            } else {
                s = pos + s;
                factor = 0;
            }
        }
        return s;
    }


    public static void main(String[] args) {
        System.out.println(new Solution1().multiply("123", "456"));
    }
}
